package com.fzy.leetcodeExam.test349;

import java.util.Arrays;

public class Main {

    public static void main(String[] args) {
        Main test = new Main();
        System.out.println(test.smallestString("leetcode"));
    }

    public int findNonMinOrMax(int[] nums) {

        if(nums.length <=2){
            return -1;
        }
        Arrays.sort(nums);
        int min = nums[0];
        int max = nums[nums.length-1];

        for(int i = 0;i<nums.length-1;i++){
            if(nums[i] != min && nums[i] != max){
                return nums[i];
            }
        }
        return -1;
    }

    public String smallestString(String s) {
        char[] str = s.toCharArray();
        int n = str.length;

        //开始的位置
        int index = 0;
        for(int i = 0;i<n;i++){
            if(str[i] == 'a'){
                index = i;
                if(index == str.length-1){
                    str[n-1] = 'z';
                    return String.valueOf(str);
                }
            }else {
                break;
            }
        }
        //结束的位置
        int end = str.length;
        for(int i = 1;i<n;i++){
            if(str[i-1] != 'a' && str[i] == 'a'){
                end =  i;
                break;
            }
        }

        for(int i = index;i<end;i++){
            if(str[i] != 'a'){
                str[i]--;
            }
        }
        return String.valueOf(str);
    }

    public long minCost(int[] nums, int x) {
        int[] min = new int[nums.length];
        long sum = 0;

        //求和，初始化最小值数组
        for(int i = 0;i<nums.length;i++){
            sum += nums[i];
            min[i] = nums[i];
        }

        long ret = sum;

        for(int i = 1;i<=nums.length;i++){
            for(int j = 0;j<nums.length;j++){
                int t = Math.min(min[j],nums[(j+i) % nums.length]);
                sum -= min[j];
                sum += t;
                min[j] = t;
            }
            ret = Math.min(ret,sum + (long) x*i);
        }
        return ret;
    }
}
